package com.common.util;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateTimeUtil {

    private static final String YMDHMS_DATE_FORMAT_T = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    private static final String ISO8601_DATE_FORMAT_T = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    private static final String YMDHMS_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String YMDHMS_DATE_FORMAT_V1 = "yyyy/MM/dd HH:mm:ss";

    private static final String CSV_DATE_FORMAT = "yyyyMMddHHmmss";
    private static final String YMD_DATE_FORMAT = "yyyy-MM-dd";
    private static final String YYYYMMDDHHMMSS_DATE_FORMATE= "yyyyMMddHHmmssSSS";
    /*** 1毫秒 */
    public static final long ONE_MILLI_SECOND = 1l;
    /*** 1秒 */
    public static final long ONE_SECOND = ONE_MILLI_SECOND * 1000;
    /*** 1分钟 */
    public static final long ONE_MINUTE = ONE_SECOND * 60;
    /*** 1小时 */
    public static final long ONE_HOUR = ONE_MINUTE * 60;
    /*** 1天 */
    public static final long ONE_DAY = ONE_HOUR * 24;


    /**
     * 根据日期时间字符串获取日期时间
     * @param datetime
     * @param format 为null或者空时默认为yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static LocalDateTime parseByFormat(@NotNull String datetime,String format) {

        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(datetime,formatter);
    }


    /**
     * 获取指定格式的日期字符串
     * @param dateTime
     * @param format 为null或者空时默认为yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateTime(@NotNull LocalDateTime dateTime, String format) {
        //时间转字符串格式化
        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format, Locale.SIMPLIFIED_CHINESE);
        return dateTime.format(formatter);
    }


    /**
     * LocalDateTime 转为 Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime){
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Date 转 LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        return LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());
    }

    /**
     * 根据时间获取时间戳
     * @param dateTime
     * @return
     */
    public static Long dateTimeToTimestamp(LocalDateTime dateTime) {

        if (dateTime == null) {
            return null;
        }
        return dateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }


    /**
     * 根据时间戳获取日期时间
     * @param timestamp
     * @return
     */
    public static LocalDateTime timestampToDateTime(Long timestamp) {
        return timestamp==null?null:LocalDateTime.ofInstant( Instant.ofEpochMilli(timestamp),ZoneId.systemDefault());
    }


    /**
     * 获取当日开始时间0点
     */
    public static LocalDateTime todayStartTime(){
        return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
    }

    /**
     * 获取当日结束时间0点
     * @return
     */
    public static LocalDateTime todayEndTime(){
        return  LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
    }

    public static String getGMTTime() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(c.getTime());
    }


}